-- Version 0.7 WIP
-- TODO
--  add options for build command grouping
function widget:GetInfo()
	return {
		name      = 'Funks Simple Selection Menu',
		desc      = 'Shows current selections build, order, and state options',
		author    = 'Funkencool',
		date      = 'Sep 2013', -- 2014
		license   = 'GNU GPL v2',
		layer     = 0,
		enabled   = true,
		handler   = true,
	}
end

-- Includes --
local cat, ignoreCMDs = include('Configs/buildMenu.lua') --categories
local catNames = {'ECONOMY', 'DEFENSE', 'INTEL', 'FACTORIES', 'BUILD'} -- Must be the same as cat indexes
local imageDir = 'luaui/images/buildIcons/'
--------------

-- Config --
local nCol, nRow = 4, 2
------------
local unit = {n = 0}
local grid = {}

-- Chili vars --
local choiceA
local Chili, buildMenu, window
local panH, panW, winW, winH, winX, winB, tabH, minMapH, minMapW
----------------


-- Spring Functions --
local spGetTimer          = Spring.GetTimer
local spDiffTimers        = Spring.DiffTimers
local spGetActiveCmdDesc  = Spring.GetActiveCmdDesc
local spGetActiveCmdDescs = Spring.GetActiveCmdDescs
local spGetActiveCommand  = Spring.GetActiveCommand
local spGetCmdDescIndex   = Spring.GetCmdDescIndex
local spGetFullBuildQueue = Spring.GetFullBuildQueue
local spGetSelectedUnits  = Spring.GetSelectedUnits
local spSendCommands      = Spring.SendCommands
local spSetActiveCommand  = Spring.SetActiveCommand

local echo = Spring.Echo
----------------------


-- Local vars --
local initialized = false
local updateRequired = true
local updateTab = true
local selectedUnits = {}
local oldTimer = spGetTimer()
local r,g,b = Spring.GetTeamColor(Spring.GetMyTeamID())
local teamColor = {r,g,b}
----------------
local buildNum = 0

----------------------------------------------------------
local function showGrid(show)
	window.selected = show
	for i = 1, #grid do
		if i == show and not grid[i].visible then
			grid[i]:Show()
		elseif i ~= show and grid[i].visible then
			grid[i]:Hide()
		end
	end
end

local function nextPage()
	local lastPage = math.floor(buildNum / (nCol * nRow)) + 1
	local page = window.selected
	page = page + 1
	if page > lastPage then page = 1 end
	showGrid(page)
	echo(page)
end

local function prevPage()
	local lastPage = math.floor(buildNum / (nCol * nRow)) + 1
	local page = window.selected
	page = page - 1
	if page < 1 then page = lastPage end
	showGrid(page)
	echo(page)
end
----------------------------------------------------------
----------------------------------------------------------

local function addBuild(name)
	local i = math.floor(buildNum / (nCol * nRow)) + 1
	grid[i]:AddChild(unit[name])
	buildNum = buildNum + 1
end

local function addState(cmd)
	
end

local function addOrder(cmd)
	
end

local oldList = {}
local function ParseCMDs()
	local cmdList = spGetActiveCmdDescs()
	local newUnit = not (#oldList == #cmdList)
	
	if newUnit then
		for i = 1, #grid do
			grid[i]:ClearChildren()
		end
		showGrid(1)
		oldList = cmdList
		buildNum = 0
	end
	
	for i = 1, #cmdList do
		local cmd = cmdList[i]
		
		if cmd.name == '' then 
			return 
		end
		
		if cmd.id < 0 and newUnit then
			addBuild(cmd.name)
		elseif #cmd.params > 1 then
			addState(cmd)
		else
			addOrder(cmd)
		end
		
	end
end
---------------------------------------------------------

-------------------------- 
-- Iniatilizes main/permanent chili controls
--  These are then shown/hidden when needed
function widget:Initialize()
	widgetHandler:ConfigLayoutHandler(false)
	Spring.ForceLayoutUpdate()
	spSendCommands({'tooltip 0'})
	
	if (not WG.Chili) then
		widgetHandler:RemoveWidget()
		return
	end
	
	Chili = WG.Chili
	local screen0 = Chili.Screen0
	
	-- WIP to customize layout
	
	-- config --
	local vertical = true 
	local minMapBottom = true 
	------------
	
	-- these numbers are used by numerous chili widgets
	--  and should probably be an include or global object
	local scrH = screen0.height
	local tabB = 0
	local ordH = scrH * 0.05
	local selH = scrH * 0.2
	local selW = selH
	minMapH = scrH * 0.3
	minMapW = minMapH * Game.mapX/Game.mapY
	
	if (Game.mapX/Game.mapY > 1) then
		minMapW = minMapH *(Game.mapX/Game.mapY)^0.5
		minMapH = minMapW * Game.mapY/Game.mapX
	end
	
	if minMapBottom then
		selW = minMapW
		selH = minMapH
	end
	
	if vertical then
		local tempRow = nRow
		nRow = nCol
		nCol = tempRow
		winH = scrH * 0.5
		winW = (winH - 25) * nCol / nRow
		tabH = winH/3
		winX = 0
		winB = selH
		tabB = winB + selH
	else
		winH = scrH * 0.15
		winW = (winH - 25) * nCol / nRow
		tabH = winH
		winX = selW
		winB = 0
	end
	
	-------------------------------------------------------

	window = Chili.Window:New{
		parent   = screen0,
		x        = winX,
		bottom   = winB,
		width    = winW,
		height   = winH,
		selected = 1,
		padding  = {0,0,0,0},
		children = {
			Chili.Button:New{
				caption = '<--',
				x       = 5,
				y       = 5,
				width   = '40%',
				height  = 20,
				OnMouseUp = {prevPage},
			},
			Chili.Button:New{
				caption = '-->',
				right   = 5,
				y       = 5,
				width   = '40%',
				height  = 20,
				OnMouseUp = {nextPage},
			},
		}	
	}

	-- Creates a cache of buttons
	for name, data in pairs(UnitDefNames) do
		unit[name] = Chili.Button:New{
			name      = name,
			caption   = name,
			padding   = {0,0,0,0},
			margin    = {0,0,0,0},
			OnMouseUp = {function(self) self:Hide() end},
			children  = {
				Chili.Image:New{
					height = '100%',
					width  = '100%',
					file   = #..-data.id,
				},
			}
		}
		unit.n = unit.n + 1
	end

	for i = 1, math.floor(unit.n / (nCol * nRow)) + 1 do 
		grid[i] = Chili.Grid:New{
			parent  = window,
			rows    = nRow,
			columns = nCol,
			x       = 0,
			y       = 25,
			right   = 0,
			bottom  = 0,
			padding = {0,0,0,0},
		}
	end
	
end

local update
function widget:CommandsChanged()
	update = true
end

function widget:Update()
	if update then
		ParseCMDs()
		update = false
	end
end

function widget:Shutdown()

end			